﻿using DataBase;
using DataBase.From;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WebDataGet.Utils;
using static WebDataGet.Models.WebDataJson;

namespace WebDataGet.Services
{
    public class WebDataGetService
    {
        private NlogService NlogService { get; set; }

        private ConfigService ConfigService { get; set; }

        private FreeSqlService FreeSqlService { get; set; }
        public WebDataGetService() { }

        public WebDataGetService(NlogService nlogService, ConfigService configService, FreeSqlService freeSqlService)
        {
            NlogService = nlogService;
            ConfigService = configService;
            FreeSqlService = freeSqlService;
        }


        public async Task LoopGet()
        {
            while (true)
            {

                try
                {
                    foreach (var item in ConfigService.ExcelDatas)
                    {
                        GetData(item.Iemi,item.TerminalId);
                    }
                }
                catch (Exception ex)
                {
                    NlogService.Error(ex.ToString());

                }
                NlogService.Debug("数据插入成功，休眠60s");
                await Task.Delay(60 * 1000);
            }
        }


        public async Task GetData(string imei, string TerminalId)
        {
            var api = "/grpt/api/client/grptapp/queryDeviceStatus";

            MyHttpHelper myHttpHelper = new MyHttpHelper(ConfigService.Setting.Ip);
            var data = await myHttpHelper.JsonHttpGet<Rootobject>(api, new Dictionary<string, string>()
            {
                {"companyId",ConfigService.Setting.CompanyId.ToString()},
                {"grptToken",ConfigService.Setting.GrptToken },
                {"imei" ,imei},
                {"pageNum","1" },
                {"pageSize","1" }
            });

            NlogService.Info(JsonConvert.SerializeObject(data));
            NlogService.Debug("转化数据类型");
            var targetData = data.data.list[0];
            var insertModel = new TManholeCoverDaLian()
            {
                DataId = targetData.id,
                CreateTime = DateTime.Now,
                Time = DateTime.Parse(targetData.time),
                ChgasValue = targetData.chgasValue,
                CogasAlarm = targetData.chgasAlarm,
                DefenseStatus = targetData.defenseStatus,
                HsgasAlarm = targetData.hsgasAlarm,
                HsgasValue = targetData.hsgasValue,
                CogasValue = targetData.cogasValue,
                OgasAlarm = targetData.ogasAlarm,
                OgasValue = targetData.cogasValue,
                ShakeAlarm = targetData.shakeAlarm,
                PastNumber = targetData.pastNumber,
                SignalStatus = targetData.signalStatus,
                SignalValue = targetData.signalValue,
                StateStr = targetData.stateStr,
                TempStatus = targetData.tempStatus,
                VoltageStatus = targetData.voltageStatus,
                WaterStatus = targetData.waterStatus,
                WellLidStatus = targetData.wellLidStatus,
                WakeupTime = targetData.wakeupTime
            };
            insertModel.TerminalId = TerminalId;
            insertModel.DeviceId = Convert.ToInt64(TerminalId, 16);

            insertModel.VoltageValue = Convert.ToInt32(new Regex("%").Split(targetData.voltageValue)[0]);
            insertModel.WaterValue = Convert.ToInt32(new Regex("CM").Split(targetData.waterValue)[0]);
            insertModel.TempValue = Convert.ToInt32(new Regex("℃").Split(targetData.tempValue)[0]);
            insertModel.Snr = Convert.ToInt32(new Regex("dBm").Split(targetData.snr)[0]);
            insertModel.Rsrp = Convert.ToInt32(new Regex("dBm").Split(targetData.rsrp)[0]);
            insertModel.BatteryVoltage = Convert.ToDecimal(new Regex("V").Split(targetData.batteryVoltage)[0]);
            insertModel.WellLidangle = Convert.ToInt32(new Regex("°").Split(targetData.wellLidangle)[0]);
            insertModel.Rsrq = Convert.ToInt32(new Regex("dBm").Split(targetData.rsrq)[0]);

            NlogService.Debug(JsonConvert.SerializeObject(insertModel));
            await FreeSqlService.From.Insert(insertModel).ExecuteAffrowsAsync();
            NlogService.Info($"数据[{TerminalId}]插入成功！");
        }


    }
}
